<?php

require_once 'Zend/Loader.php';
require_once 'models/Paciente.php';

/**
 * This class provides a starting point for the Zend Framework MVC and general environemnt setup
 */
class Bootstrap {
	private $frontController = null;
	private $root;
		
	public function __construct($root) {
		
		Zend_Loader::registerAutoload();
         
         $this->root = $root;

         $this->setupEnvironment();
         $this->setupRegistry();
         $this->readConfig();
         $this->setupDb();
		 $this->setupMVC();
		 $this->readLayout();
		 $this->setupIdentity();
	}
	
	public function run() {
      	$response = $this->frontController->dispatch();
        $this->sendResponse($response);		
	}

	/**
	 * Setup PHP environemnt 
	 */
	private function setupEnvironment() {
      	error_reporting(E_ALL|E_STRICT);
        ini_set('display_errors', true);
        date_default_timezone_set('America/Buenos_Aires');
		Zend_Locale::setDefault('es_AR');
	}
	
	/**
	 * Setup MVC infrastructure 
	 */
	private function setupMVC() {
		$this->setupFrontController();
		$this->setupView();
	}
	
	/*
	 * Setup controllers path and behaviour
	 */
	private function setupFrontController() {
        $this->frontController = Zend_Controller_Front::getInstance();
        $this->frontController->throwExceptions(true);
        $this->frontController->returnResponse(true);
        $this->frontController->addModuleDirectory($this->root.'/application/modules');
	}
	
	/*
	 * Setup view and layout behaviour
	 */
	private function setupView() {
		
		// Makes pages rendered are in UTF-8 encoding
        $view = new Zend_View;
        $view->setEncoding('UTF-8');
        $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view); 
        Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
        
        // Dojo
       $view->addHelperPath($this->root. '/library/Zend/Dojo/View/Helper', 'Zend_Dojo_View_Helper');
       $view->dojo()->addStylesheet('/js/dojo-zend/dojo/resources/dojo.css');
       $view->dojo()->addStylesheet('/js/dojo-zend/dijit/themes/tundra/tundra.css');
       $view->dojo()->addStylesheet('/js/dojo-zend/dijit/demos/mail/mail.css');
       $view->dojo()->addStylesheet('/css/prueba.css');
       
       $view->dojo()->setLocalPath('/js/dojo-zend/dojo/dojo.js');
       
       $view->dojo()->requireModule('dojo.parser');
       $view->dojo()->requireModule('dijit.Toolbar');
       $view->dojo()->requireModule('dijit.layout.LayoutContainer');
       $view->dojo()->requireModule('dijit.layout.AccordionContainer');
       $view->dojo()->requireModule('dijit.layout.ContentPane');
       $view->dojo()->requireModule('dijit.form.DateTextBox');
       $view->dojo()->requireModule('dijit.Menu');
       $view->dojo()->requireModule('dijit.Tooltip');
       $view->dojo()->requireModule('dijit.Dialog');
       $view->dojo()->requireModule('dijit.form.ComboBox');
       $view->dojo()->requireModule('dijit.form.CheckBox');
       $view->dojo()->requireModule('dijit.form.FilteringSelect');
       $view->dojo()->requireModule('dijit.form.Textarea');
       $view->dojo()->requireModule('dijit.layout.SplitContainer');
       $view->dojo()->requireModule('dijit.layout.TabContainer');
       

  		$view->addHelperPath( 
            $this->root. '/library/View/Helper',
            'View_Helper_'
        );        
        
        Zend_Layout::startMvc(array(
                'layoutPath' => $this->root. '/application/layouts',
                'layout' => 'common'
        ));        
	}
	
  	private function sendResponse(Zend_Controller_Response_Http $response) {
        $response->setHeader('Content-Type', 'text/html; charset=UTF-8', true);
        $response->sendResponse();
    }
    
	/*
	 * Setup application registry 
	 */
    private function setupRegistry() {
    	// Set the application registry to work as an object instead of an array
        $registry = new Zend_Registry(array(), ArrayObject::ARRAY_AS_PROPS);
        Zend_Registry::setInstance($registry);    
    }
    
    /*
     * Read configuration and store in the applciation registry 
     */
    private function readConfig() {
    	$config = new Zend_Config_Ini($this->root . '/application/Config.ini', 'general');
        Zend_Registry::getInstance()->configuration = $config;    
    }
    
	private function readLayout() {
    	$decorators = array();
		$decorators['lista_ul']['elements'] = array(
		array('ViewHelper'),
		array('Description'),
		array('Errors'),
		array('Label', array('tag' => null, 'separator'=>' ')),
		array('HtmlTag', array('tag' => 'li', 'class'=>'element-group')),
		);
		$decorators['lista_ul']['form'] = array(
		array('FormElements'),
		array('HtmlTag', array('tag' => '<ul>')),
		array('Form'));
		$decorators['label_noerrors']['elements'] = array(
		array('ViewHelper'),
		array('Label', array('tag' => null, 'separator'=>' '))
		);
		$decorators['label_noerrors']['form'] = array(
		array('FormElements'),
		array('HtmlTag', array('tag' => null)),
		array('Form'));
        Zend_Registry::getInstance()->DECORATORS = $decorators;    
    }
    
    /*
     * Setup database connection and table access behaviour
     */
    private function setupDb() {
		$config = Zend_Registry::getInstance()->configuration;
        $db = Zend_Db::factory($config->db->adapter, $config->db->toArray());
        $db->query("SET NAMES 'utf8'");
        
        Zend_Registry::getInstance()->database = $db;
        Zend_Db_Table::setDefaultAdapter($db);
    }
    
    /*
     * Register's current user credentials
     */
    private function setupIdentity() {
    	$identity = Zend_Auth::getInstance()->getIdentity();
    	if ($identity == null) {
    		Zend_Registry::getInstance()->user=false;
    		Zend_Registry::getInstance()->admin=false;
    	}
    	else {
    		Zend_Registry::getInstance()->identity=$identity;
    		Zend_Registry::getInstance()->admin=false;
    		Zend_Registry::getInstance()->user=true;
    	}
    }

	 
}

?>